<template>
  <div>
    <Searchbar>
      <el-input @change="getList" v-model="search.id" placeholder="请输入id"></el-input>
      <el-select
        @change="onQuestionTypeChange"
        v-model="search.question_type"
        placeholder="请选择题目类型"
      >
        <el-option
          v-for="item in question_types"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-input @change="getList" v-model="search.question_name" placeholder="请输入名称"></el-input>
      <el-button class="search-btn" @click="getList" type="primary">查询</el-button>
    </Searchbar>

    <TableBar>
      <el-button @click="goCreate" type="primary" icon="el-icon-plus">新建</el-button>
      <el-button @click="onShowUpload(1)" icon="el-icon-upload">导入选择题</el-button>
      <el-button @click="onShowUpload(2)" icon="el-icon-upload">导入简答题</el-button>
      <el-button @click="onMulDel" icon="el-icon-del">批量删除</el-button>
    </TableBar>

    <el-table :data="tableData" @selection-change="handleSelectionChange" style="width: 100%">
      <el-table-column fixed="left" type="selection" width="55"></el-table-column>
      <el-table-column fixed="left" type="index" label="序号" width="55"></el-table-column>
      <el-table-column prop="id" label="ID"></el-table-column>
      <el-table-column prop="question_name" label="名称"></el-table-column>
      <el-table-column prop="knowledges" label="知识点列表"></el-table-column>
      <el-table-column prop="question_type_label" label="题目类型"></el-table-column>
      <el-table-column prop="create_time" label="创建时间"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" @click="goEdit(scope.row)" size="small" >编辑</el-button>
          <el-button type="warning" @click="onDel(scope.row)" size="small" >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next, jumper"
      @current-change="handleCurrentChange"
      @size-change=handleSizeChange
      :current-page.sync="search.page"
      :page-size="search.page_size"
      :total="total"
    ></el-pagination>
    <MulUpload action="/object/batch/create/" type="7" temp="13" title="导入选择题" @onClose="onHideUpload" :show="showUpload1" />
    <MulUpload action="/object/batch/create/" type="8" temp="14" title="导入简答题" @onClose="onHideUpload" :show="showUpload2" />
  </div>
</template>
<script>
import MulUpload from "@/components/MulUpload";
import CreateFilter from "@/mixins/CreateFilter";
import showEditMixin from "@/mixins/showEditMixin";
import uploadMixin from "@/mixins/uploadMixin";
import pageMixin from "@/mixins/pageMixin";
import { queModel } from "@/api/models";
import Searchbar from "@/components/Searchbar";
import TableBar from "@/components/TableBar";
import selectionChang from '@/mixins/selectionChang.js';
// import {Wconfirm} from '@/utils/utils.js';
import {Message, MessageBox} from 'element-ui';
import {WConfirm} from '@/utils/utils.js';
export default {
  mixins: [CreateFilter, showEditMixin, uploadMixin, pageMixin, selectionChang],
  components: { MulUpload, Searchbar, TableBar },
  data() {
    return {
      tableData: [],
      total: 0,
      search: {
        id: null,
        question_type: null,
        question_name: null,
        page: 1,
        page_size: 10,
      },
      showUpload1: false,
      showUpload2: false
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    onHideUpload() {
      this.showUpload1 = false; 
      this.showUpload2 = false; 
    },
    onShowUpload(col) {
      if(col == 1) {
        this.showUpload1 = true;
      }else{
        this.showUpload2 = true;
      }
    },
    goCreate() {
      this.$router.push('/create/ques/');
    },
    goEdit(row) {
      this.$router.push('/edit/ques/' + row.id + '/' + row.question_type);
    },
    getList() {
      console.log("getList");
      queModel.list(
        this.search,
        (res) => {
          this.total = res.total_records;
          this.tableData = res.data.map((v) => {
            v.question_type_label = this.question_typeToLabel(v.question_type);
            return v;
          });
        },
        (err) => {
          console.log(err);
        }
      );
    },
    onDel(row) {
      console.log(row)
      MessageBox.confirm('确定删除题目').then(() => {
          queModel.batchRem({
            status: 2,
            questions: [row.id]
          }, res => {
            Message.success(res.mesage);
            this.getList();
          })
        }).catch(() => {
          Message.warning('操作已取消')
      })
    },
    onMulDel() {
      if(this.multipleSelection.length == 0) {
        return Message.warning('请选择要处理班级');
      }
      let ids = this.multipleSelection.map((v) => {
        return v.id;
      })
      WConfirm('确定批量删除题目', () => {
        queModel.batchRem({
          questions: ids,
          status: 2,
        },(res) => {
          Message.success(res.message)
          this.getList();
        }) 
      }, (err) => {
        console.log(err)
        Message.warning('操作取消')
      })
    }
  },
};
</script>
<style lang="scss">
</style>